<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Block_Post_Toolbar extends Block_Admin_Page_Toolbar {

    protected $_frameLength = 5;
    protected $_frameInited = false;
	protected $_frameStart;
	protected $_frameEnd;
	
	protected $_orderVarName        = 'order';
	protected $_directionVarName    = 'dir';

	public function setCollection($collection) {
		parent::setCollection($collection);
		// apply sort
		$collection->getSelect()->order($this->getCurrentOrder(), $this->getCurrentDirection()=='asc');
	}
	
	public function canShowFirst() {
		return $this->getFrameStart() > 1;
	}
	
	public function canShowPrevious() {
		return $this->getCurrentPage() > 1;
	}
	
	public function canShowNext() {
		return $this->getCurrentPage() < $this->getPageCount();
	}
	
	public function canShowLast() {
		return $this->getFrameEnd() < $this->getPageCount();
	}
	
	public function getFirstPageUrl() {
		return $this->getPageUrl(1);
	}
	
	public function getPreviousPageUrl() {
		return $this->getPageUrl($this->getCurrentPage()-1);
	}
	
	public function getNextPageUrl() {
		return $this->getPageUrl($this->getCurrentPage()+1);
	}
	
	public function getLastPageUrl() {
		return $this->getPageUrl($this->getPageCount()+1);
	}
	
	public function getFrameStart() {
		$this->_initFrame();
		return $this->_frameStart;
	}

	public function getFrameEnd() {
		$this->_initFrame();
		return $this->_frameEnd;
	}
	
	protected function _initFrame() {
		if (!$this->_frameInited) {
			$this->_frameStart = max(1, $this->getCurrentPage() - $this->_frameLength);
			$this->_frameEnd = min($this->getPageCount(), $this->getCurrentPage() + $this->_frameLength);
		}
	}
	
	public function setFrameLength($frameLength) {
		$this->_frameLength = $frameLength;
		return $this;
	}

	public function getCurrentOrder() {
		$cookie = App::objectSingleton('Core_Cookie');
		if ($this->getData('order')==null) {
			if ($order = App::get($this->_orderVarName)) {
				$cookie->set('order', $order);
			} else {
				$order = $cookie->get('order') ? $cookie->get('order') : $this->getDefaultOrder();
			}
			$this->setData('order', $order);
		}
		return $this->getData('order');
	}
	
	public function getCurrentDirection() {
		$cookie = App::objectSingleton('Core_Cookie');
		if ($this->getData('direction')==null) {
			if ($direction = App::get($this->_directionVarName)) {
				$cookie->set('direction', App::get($this->_directionVarName));
			} else {
				$direction = $cookie->get('direction') ? $cookie->get('direction') : 'asc';
			}
			$this->setData('direction', $direction);
		}
		return $this->getData('direction');
	}

	public function isOrderCurrent($val) {
		return $val == $this->getCurrentOrder();
	}
	
	public function getAvailableOrders() {
		return array(
			'title' => 'Title',
			'rating' => 'Rating',
			'update_time' => 'Time'
		);
	}

	public function getDefaultOrder() {
		return 'title';
	}
	
	public function getAvailableLimit() {
		return array(5, 15, 30);
	}
	
	public function getDefaultLimit() {
		return 5;
	}
	
	public function getOrderUrl($order, $dir = 'asc') {
		$query = App::get();
		$query[$this->_orderVarName] = $order;
		$query[$this->_directionVarName] = $dir;
		return App::url('*/*/*', array('_query' => $query));
	}
}